home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Archivers / PPCUnARJ / source / environ.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-07  |  12.7 KB  |  746 lines

  1. /* ENVIRON.C, UNARJ, R JUNG, 09/01/91
  2.  * Implementation dependent routines
  3.  * Copyright (c) 1991 by Robert K Jung.  All rights reserved.
  4.  *
  5.  *   This code may be freely used in programs that are NOT ARJ archivers
  6.  *   (both compress and extract ARJ archives).
  7.  *
  8.  *   If you wish to distribute a modified version of this program, you
  9.  *   MUST indicate that it is a modified version both in the program and
  10.  *   source code.
  11.  *
  12.  *   If you modify this program, I would appreciate a copy of the new
  13.  *   source code.  I am holding the copyright on the source code, so
  14.  *   please do not delete my name from the program files or from the
  15.  *   documentation.
  16.  *
  17.  *   The UNIX file date-time stamping code is derived from ZOO by
  18.  *   Rahul Dhesi.
  19.  *
  20.  * Modification history:
  21.  * Date      Programmer  Description of modification.
  22.  * 04/09/91  R. Jung     Rewrote code.
  23.  * 04/23/91  M. Adler    Portabilized.
  24.  * 04/29/91  R. Jung     Added get_mode_str().
  25.  * 05/08/91  R. Jung     Combined set_ftime() and set_fmode().
  26.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  27.  *                       set_ftime_mode().
  28.  * 07/07/91  R. Jung     Added default_case_path() and UNIX section.
  29.  * 07/24/91  R. Jung     Fixed use of _chmod to handle directories.
  30.  * 08/27/91  R. Jung     Added date/time handling to Coherent.
  31.  * 09/01/91  R. Jung     Added #include <stdlib.h> to vanilla section.
  32.  *                       Added file date-time stamping to UNIX section.
  33.  * 02/07/98  A. Kleinert added some typecasts for SAS/C PPC
  34.  *                       uncommented no-op case path and ftime_mode functions
  35.  *
  36.  */
  37.  
  38. #include "unarj.h"
  39.  
  40. #ifdef __TURBOC__
  41.  
  42. #define SUBS_DEFINED
  43.  
  44. #include <string.h>
  45. #include <dos.h>
  46. #include <io.h>
  47. #include <fcntl.h>
  48. #include <alloc.h>
  49.  
  50. FILE *
  51. file_open(name, mode)
  52. char *name;
  53. char *mode;
  54. {
  55.     return fopen(name, mode);
  56. }
  57.  
  58. int
  59. file_read(buf, size, nitems, stream)
  60. char *buf;
  61. int  size;
  62. int  nitems;
  63. FILE *stream;
  64. {
  65.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  66. }
  67.  
  68. int
  69. file_seek(stream, offset, mode)
  70. FILE *stream;
  71. long offset;
  72. int  mode;
  73. {
  74.     return fseek(stream, offset, mode);
  75. }
  76.  
  77. long
  78. file_tell(stream)
  79. FILE *stream;
  80. {
  81.     return ftell(stream);
  82. }
  83.  
  84. int
  85. file_write(buf, size, nitems, stream)
  86. char *buf;
  87. int  size;
  88. int  nitems;
  89. FILE *stream;
  90. {
  91.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  92. }
  93.  
  94. voidp *
  95. xmalloc(size)
  96. int size;
  97. {
  98.     return (voidp *)malloc((size_t) size);
  99. }
  100.  
  101. void
  102. case_path(name)
  103. char *name;
  104. {
  105.     strupper(name);
  106. }
  107.  
  108. void
  109. default_case_path(name)
  110. char *name;
  111. {
  112.     strupper(name);
  113. }
  114.  
  115. int
  116. file_exists(name)
  117. char *name;
  118. {
  119.     return (access(name, 0) == 0);
  120. }
  121.  
  122. void
  123. get_mode_str(str, mode)
  124. char *str;
  125. uint mode;
  126. {
  127.     strcpy(str, "---W");
  128.     if (mode & FA_ARCH)
  129.         str[0] = 'A';
  130.     if (mode & FA_SYSTEM)
  131.         str[1] = 'S';
  132.     if (mode & FA_HIDDEN)
  133.         str[2] = 'H';
  134.     if (mode & FA_RDONLY)
  135.         str[3] = 'R';
  136. }
  137.  
  138. int
  139. set_ftime_mode(name, tstamp, attribute, host)
  140. char  *name;
  141. ulong tstamp;
  142. uint  attribute;
  143. uint  host;
  144. {
  145.     FILE *fd;
  146.     int code;
  147.  
  148.     if ((fd = fopen(name, "r+b")) == NULL)
  149.         return -1;
  150.     code = setftime(fileno(fd), (struct ftime *) &tstamp);
  151.     fclose(fd);
  152.     if (host == OS)
  153.     {
  154.         attribute &= 0x27;
  155.         if (_chmod(name, 1, attribute) == -1)
  156.             return -1;
  157.     }
  158.     return code;
  159. }
  160.  
  161. #endif
  162.  
  163. #ifdef _QC
  164.  
  165. #define SUBS_DEFINED
  166.  
  167. #include <string.h>
  168. #include <dos.h>
  169. #include <io.h>
  170. #include <fcntl.h>
  171. #include <malloc.h>
  172.  
  173. FILE *
  174. file_open(name, mode)
  175. char *name;
  176. char *mode;
  177. {
  178.     return fopen(name, mode);
  179. }
  180.  
  181. int
  182. file_read(buf, size, nitems, stream)
  183. char *buf;
  184. int  size;
  185. int  nitems;
  186. FILE *stream;
  187. {
  188.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  189. }
  190.  
  191. int
  192. file_seek(stream, offset, mode)
  193. FILE *stream;
  194. long offset;
  195. int  mode;
  196. {
  197.     return fseek(stream, offset, mode);
  198. }
  199.  
  200. long
  201. file_tell(stream)
  202. FILE *stream;
  203. {
  204.     return ftell(stream);
  205. }
  206.  
  207. int
  208. file_write(buf, size, nitems, stream)
  209. char *buf;
  210. int  size;
  211. int  nitems;
  212. FILE *stream;
  213. {
  214.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  215. }
  216.  
  217. voidp *
  218. xmalloc(size)
  219. int size;
  220. {
  221.     return (voidp *)malloc((size_t) size);
  222. }
  223.  
  224. void
  225. case_path(name)
  226. char *name;
  227. {
  228.     strupper(name);
  229. }
  230.  
  231. void
  232. default_case_path(name)
  233. char *name;
  234. {
  235.     strupper(name);
  236. }
  237.  
  238. int
  239. file_exists(name)
  240. char *name;
  241. {
  242.     return (access(name, 0) == 0);
  243. }
  244.  
  245. void
  246. get_mode_str(str, mode)
  247. char *str;
  248. uint mode;
  249. {
  250.     strcpy(str, "---W");
  251.     if (mode & FA_ARCH)
  252.         str[0] = 'A';
  253.     if (mode & FA_SYSTEM)
  254.         str[1] = 'S';
  255.     if (mode & FA_HIDDEN)
  256.         str[2] = 'H';
  257.     if (mode & FA_RDONLY)
  258.         str[3] = 'R';
  259. }
  260.  
  261. int
  262. set_ftime_mode(name, tstamp, attribute, host)
  263. char  *name;
  264. ulong tstamp;
  265. uint  attribute;
  266. uint  host;
  267. {
  268.     FILE *fd;
  269.     int code;
  270.     uint date_stamp, time_stamp;
  271.  
  272.     date_stamp = (uint)(tstamp >> 16);
  273.     time_stamp = (uint)(tstamp & 0xFFFF);
  274.     if ((fd = fopen(name, "r+b")) == NULL)
  275.         return -1;
  276.     code = _dos_setftime(fileno(fd), date_stamp, time_stamp);
  277.     fclose(fd);
  278.     if (host == OS)
  279.     {
  280.         if (_dos_setfileattr(name, attribute))
  281.             return -1;
  282.     }
  283.     return code;
  284. }
  285.  
  286. #endif
  287.  
  288. #ifdef _OS2
  289.  
  290. #define SUBS_DEFINED
  291.  
  292. #include <string.h>
  293. #define INCL_DOSFILEMGR
  294. #include <os2.h>
  295. #include <io.h>
  296. #include <fcntl.h>
  297.  
  298. FILE *
  299. file_open(name, mode)
  300. char *name;
  301. char *mode;
  302. {
  303.     return fopen(name, mode);
  304. }
  305.  
  306. int
  307. file_read(buf, size, nitems, stream)
  308. char *buf;
  309. int  size;
  310. int  nitems;
  311. FILE *stream;
  312. {
  313.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  314. }
  315.  
  316. int
  317. file_seek(stream, offset, mode)
  318. FILE *stream;
  319. long offset;
  320. int  mode;
  321. {
  322.     return fseek(stream, offset, mode);
  323. }
  324.  
  325. long
  326. file_tell(stream)
  327. FILE *stream;
  328. {
  329.     return ftell(stream);
  330. }
  331.  
  332. int
  333. file_write(buf, size, nitems, stream)
  334. char *buf;
  335. int  size;
  336. int  nitems;
  337. FILE *stream;
  338. {
  339.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  340. }
  341.  
  342. voidp *
  343. xmalloc(size)
  344. int size;
  345. {
  346.     return (voidp *)malloc((size_t) size);
  347. }
  348.  
  349. void
  350. case_path(name)
  351. char *name;
  352. {
  353.     strupper(name);
  354. }
  355.  
  356. void
  357. default_case_path(name)
  358. char *name;
  359. {
  360.     strupper(name);
  361. }
  362.  
  363. int
  364. file_exists(name)
  365. char *name;
  366. {
  367.     return (access(name, 0) == 0);
  368. }
  369.  
  370. void
  371. get_mode_str(str, mode)
  372. char *str;
  373. uint mode;
  374. {
  375.     strcpy(str, "---W");
  376.     if (mode & FA_ARCH)
  377.         str[0] = 'A';
  378.     if (mode & FA_SYSTEM)
  379.         str[1] = 'S';
  380.     if (mode & FA_HIDDEN)
  381.         str[2] = 'H';
  382.     if (mode & FA_RDONLY)
  383.         str[3] = 'R';
  384. }
  385.  
  386. int
  387. set_ftime_mode(name, tstamp, attribute, host)
  388. char  *name;
  389. ulong tstamp;
  390. uint  attribute;
  391. uint  host;
  392. {
  393.     int code;
  394.     FDATE date_stamp;
  395.     FTIME time_stamp;
  396.     HFILE handle;
  397.     FILESTATUS info;
  398.     USHORT action;
  399.  
  400.     date_stamp.day = ts_day (tstamp);
  401.     date_stamp.month = ts_month (tstamp);
  402.     date_stamp.year = ts_year (tstamp) - 1980;
  403.     time_stamp.twosecs = ts_sec (tstamp) / 2;
  404.     time_stamp.minutes = ts_min (tstamp);
  405.     time_stamp.hours = ts_hour (tstamp);
  406.     if (DosOpen (name, &handle, &action, 0L, 0, FILE_OPEN,
  407.                  OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYREADWRITE, 0L) != 0)
  408.         return -1;
  409.     info.fdateCreation = date_stamp;
  410.     info.ftimeCreation = time_stamp;
  411.     info.fdateLastAccess = date_stamp;
  412.     info.ftimeLastAccess = time_stamp;
  413.     info.fdateLastWrite = date_stamp;
  414.     info.ftimeLastWrite = time_stamp;
  415.     info.cbFile = 0;
  416.     info.cbFileAlloc = 0;
  417.     info.attrFile = 0;
  418.     code = (int)DosSetFileInfo (handle, 1, (PBYTE)&info, sizeof (info));
  419.     (void)DosClose (handle);
  420.     if (host == OS)
  421.     {
  422.         if (DosSetFileMode (name, attribute, 0L))
  423.             return -1;
  424.     }
  425.     return code;
  426. }
  427.  
  428. #endif
  429.  
  430. #ifdef UNIX
  431.  
  432. #define SUBS_DEFINED
  433.  
  434. #include <time.h>
  435.  
  436. #ifndef time_t
  437. #define time_t long
  438. #endif
  439.  
  440. extern struct tm *localtime();
  441. extern time_t time();
  442. extern char   *strcpy();
  443. extern voidp  *malloc();
  444.  
  445. FILE *
  446. file_open(name, mode)
  447. char *name;
  448. char *mode;
  449. {
  450.     return fopen(name, mode);
  451. }
  452.  
  453. int
  454. file_read(buf, size, nitems, stream)
  455. char *buf;
  456. int  size;
  457. int  nitems;
  458. FILE *stream;
  459. {
  460.     return fread(buf, (int) size, (int) nitems, stream);
  461. }
  462.  
  463. int
  464. file_seek(stream, offset, mode)
  465. FILE *stream;
  466. long offset;
  467. int  mode;
  468. {
  469.     return fseek(stream, offset, mode);
  470. }
  471.  
  472. long
  473. file_tell(stream)
  474. FILE *stream;
  475. {
  476.     return ftell(stream);
  477. }
  478.  
  479. int
  480. file_write(buf, size, nitems, stream)
  481. char *buf;
  482. int  size;
  483. int  nitems;
  484. FILE *stream;
  485. {
  486.     return fwrite(buf, (int) size, (int) nitems, stream);
  487. }
  488.  
  489. voidp *
  490. xmalloc(size)
  491. int size;
  492. {
  493.     return (voidp *)malloc((uint) size);
  494. }
  495.  
  496. void
  497. case_path(name)
  498. char *name;
  499. {
  500.     (char *) name;
  501. }
  502.  
  503. void
  504. default_case_path(name)
  505. char *name;
  506. {
  507.     strlower(name);
  508. }
  509.  
  510. int
  511. file_exists(name)
  512. char *name;
  513. {
  514.     FILE *fd;
  515.  
  516.     if ((fd = fopen(name, "rb")) == NULL)
  517.         return 0;
  518.     fclose(fd);
  519.     return 1;
  520. }
  521.  
  522. void
  523. get_mode_str(str, mode)
  524. char *str;
  525. uint mode;
  526. {
  527.     strcpy(str, "---W");
  528.     if (mode & FA_ARCH)
  529.         str[0] = 'A';
  530.     if (mode & FA_SYSTEM)
  531.         str[1] = 'S';
  532.     if (mode & FA_HIDDEN)
  533.         str[2] = 'H';
  534.     if (mode & FA_RDONLY)
  535.         str[3] = 'R';
  536. }
  537.  
  538. long
  539. gettz()         /* returns the offset from GMT in seconds */
  540. {
  541. #define NOONOFFSET    43200L
  542. #define SEC_IN_DAY    (24L * 60L * 60L)
  543. #define INV_VALUE     (SEC_IN_DAY + 1L)
  544.     static long retval = INV_VALUE;
  545.     long now, noon;
  546.     struct tm *noontm;
  547.  
  548.     if (retval != INV_VALUE)
  549.         return retval;
  550.     now = (long) time((long *) 0);
  551.     /* Find local time for GMT noon today */
  552.     noon = now - now % SEC_IN_DAY + NOONOFFSET ;
  553.     noontm = localtime(&noon);
  554.     retval = NOONOFFSET - 60 * (60 * noontm->tm_hour - noontm->tm_min);
  555.     return retval;
  556. }
  557.  
  558. long
  559. mstonix(tstamp)
  560. ulong tstamp;
  561. {
  562.     uint date, time;
  563.     int year, month, day, hour, min, sec, daycount;
  564.     long longtime;
  565.     /* no. of days to beginning of month for each month */
  566.     static int dsboy[12] =
  567.         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  568.  
  569.     date = (uint) ((tstamp >> 16) & 0xffff);
  570.     time = (uint) (tstamp & 0xffff);
  571.     if (date == 0 && time == 0)
  572.         return 0L;
  573.  
  574.     year  = ((date >> 9) & 0x7f) + 1980;
  575.     month = (date >> 5) & 0x0f;
  576.     day   = date & 0x1f;
  577.     hour  = (time >> 11) & 0x1f;
  578.     min   = (time >> 5) & 0x3f;
  579.     sec   = (time & 0x1f) * 2;
  580.  
  581.     daycount = 365 * (year - 1970) +   /* days due to whole years */
  582.                (year - 1969) / 4 +     /* days due to leap years */
  583.                dsboy[month-1] +        /* days since beginning of this year */
  584.                day-1;                  /* days since beginning of month */
  585.  
  586.     if (year % 4 == 0 &&
  587.         year % 400 != 0 && month >= 3)  /* if this is a leap year and month */
  588.         daycount++;                     /* is March or later, add a day */
  589.  
  590.     longtime = daycount * 24L * 60L * 60L +
  591.                hour * 60L * 60L + min * 60 + sec;
  592.     return longtime;
  593. }
  594.  
  595. int
  596. set_ftime_mode(name, tstamp, attribute, host)
  597. char  *name;
  598. ulong tstamp;
  599. uint  attribute;
  600. uint  host;
  601. {
  602.     time_t m_time;
  603.     struct utimbuf
  604.     {
  605.        time_t atime;             /* New access time */
  606.        time_t mtime;             /* New modification time */
  607.     } tb;
  608.  
  609.     (char *) name;
  610.     (uint) attribute;
  611.     (uint) host;
  612.  
  613.     m_time = mstonix(tstamp) + gettz();
  614.  
  615.     tb.mtime = m_time;                  /* Set modification time */
  616.     tb.atime = m_time;                  /* Set access time */
  617.  
  618.     /* set the time stamp on the file */
  619.     return utime(name, &tb);
  620. }
  621.  
  622. #endif  /* end of UNIX section */
  623.  
  624. #ifndef SUBS_DEFINED       /* vanilla version for other compilers */
  625.  
  626. #ifdef MODERN
  627. #include <string.h>
  628. #include <stdlib.h>
  629. #else /* !MODERN */
  630. extern char *strcpy();
  631. extern voidp *malloc();
  632. #endif /* ?MODERN */
  633.  
  634. FILE *
  635. file_open(name, mode)
  636. char *name;
  637. char *mode;
  638. {
  639.     return fopen(name, mode);
  640. }
  641.  
  642. int
  643. file_read(buf, size, nitems, stream)
  644. char *buf;
  645. int  size;
  646. int  nitems;
  647. FILE *stream;
  648. {
  649.     return (int) fread(buf, (int) size, (int) nitems, stream);
  650. }
  651.  
  652. int
  653. file_seek(stream, offset, mode)
  654. FILE *stream;
  655. long offset;
  656. int  mode;
  657. {
  658.     return fseek(stream, offset, mode);
  659. }
  660.  
  661. long
  662. file_tell(stream)
  663. FILE *stream;
  664. {
  665.     return ftell(stream);
  666. }
  667.  
  668. int
  669. file_write(buf, size, nitems, stream)
  670. char *buf;
  671. int  size;
  672. int  nitems;
  673. FILE *stream;
  674. {
  675.     return (int) fwrite(buf, (int) size, (int) nitems, stream);
  676. }
  677.  
  678. voidp *
  679. xmalloc(size)
  680. int size;
  681. {
  682.     return (voidp *)malloc((uint) size);
  683. }
  684.  
  685. void
  686. case_path(name)
  687. char *name;
  688. {
  689.  /* (char *) name; */
  690. }
  691.  
  692. void
  693. default_case_path(name)
  694. char *name;
  695. {
  696.  /* (char *) name; */
  697. }
  698.  
  699. int
  700. file_exists(name)
  701. char *name;
  702. {
  703.     FILE *fd;
  704.  
  705.     if ((fd = fopen(name, "rb")) == NULL)
  706.         return 0;
  707.     fclose(fd);
  708.     return 1;
  709. }
  710.  
  711. void
  712. get_mode_str(str, mode)
  713. char *str;
  714. uint mode;
  715. {
  716.     strcpy(str, "---W");
  717.     if (mode & FA_ARCH)
  718.         str[0] = 'A';
  719.     if (mode & FA_SYSTEM)
  720.         str[1] = 'S';
  721.     if (mode & FA_HIDDEN)
  722.         str[2] = 'H';
  723.     if (mode & FA_RDONLY)
  724.         str[3] = 'R';
  725. }
  726.  
  727. int
  728. set_ftime_mode(name, tstamp, attribute, host)
  729. char  *name;
  730. ulong tstamp;
  731. uint  attribute;
  732. uint  host;
  733. {
  734. /*
  735.     (char *) name;
  736.     (ulong) tstamp;
  737.     (uint) attribute;
  738.     (uint) host;
  739. */
  740.     return 0;
  741. }
  742.  
  743. #endif  /* end of vanilla section */
  744.  
  745. /* end ENVIRON.C */
  746.